package com.springdoor.wx.filter;

import com.springdoor.wx.config.WxConfig;
import com.springdoor.wx.constant.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.CacheControl;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.concurrent.TimeUnit;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private JwtRequestInterceptor jwtRequestInterceptor;

    @Autowired
    private RefreshTokenRequestInterceptor refreshTokenRequestInterceptor;

    private String[] patterns = { "/shop/**", "/category/**" };

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        /** token验证 */
        registry.addInterceptor(jwtRequestInterceptor)
//                .addPathPatterns("/wxcli/**")
                .addPathPatterns("/**")
                .excludePathPatterns("/", "/login", "/refresh-token", "/profile/**")
                .excludePathPatterns(patterns)
        ;
        /** token刷新 */
        registry.addInterceptor(refreshTokenRequestInterceptor)
                .addPathPatterns(patterns)
                .addPathPatterns("/")
        ;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        /** 本地文件上传路径 */
        registry.addResourceHandler(Constants.RESOURCE_PREFIX + "/**")
                .addResourceLocations("file:" + WxConfig.getProfile() + "/");

        /** swagger配置 */
        registry.addResourceHandler("/swagger-ui/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
                .setCacheControl(CacheControl.maxAge(5, TimeUnit.HOURS).cachePublic());;
    }
}
